class ExpensesController < ApplicationController

  before_filter :login_required
  before_filter :initialize_categories
  before_filter :initialize_expense, :only => [:show, :edit, :update, :destroy]

  def index
    respond_to do |format|
      format.html { redirect_to :action => "by_date" }
      format.xml  { render :xml => @expenses }
      # there should be no way for the iPhone to get here
      # except by typing the URL directly
      format.iphone { redirect_to :controller => "welcome" }
    end
  end

  # GET /expenses/by_date
  # GET /expenses/by_date.xml
  def by_date
    @page_title = "Expenses [by Date]"
    @expenses = @current_user.expenses.find(:all)

    # attempt to find expenses by a date range
    if !(params[:start].nil? and params[:finish].nil?)
      begin
        @expenses = @current_user.expenses.find(:all, :conditions => {
          :created_at => params[:start].to_date .. params[:finish].to_date
        })
      # capture invalid or nil date ranges
      rescue
        flash[:error] = "Invalid dates in date range."
        @expenses = nil
      end
    end

    respond_to do |format|
      format.html # by_date.html.erb
      format.xml  { render :xml => @expenses }
      format.iphone do # by_date.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # GET /expenses/by_category
  # GET /expenses/by_category.xml
  def by_category
    @page_title = "Expenses [by Category]"
    @categories = @current_user.categories.find(:all)

    respond_to do |format|
      format.html # by_category.html.erb
      format.xml  { render :xml => @categories }
      format.iphone do # by_category.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # GET /expenses/new
  # GET /expenses/new.xml
  def new
    @page_title = "New Expense"
    @expense = @current_user.expenses.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @expense }
      format.iphone do # new.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # POST /expenses
  # POST /expenses.xml
  def create
    @expense = @current_user.expenses.create(params[:expense])
    params[:expense][:categories].each do |category_id|
      category_id = category_id.to_i
      if category_id > 0
        @expense.categories << Category.find(category_id)
      end
    end
    respond_to do |format|
      if @expense.save
        flash[:notice] = 'Expense was successfully created.'
        format.html { redirect_to(@expense) }
        format.xml  { render :xml => @expense, :status => :created, :location => @expense }
        format.iphone { redirect_to(@expense) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @expense.errors, :status => :unprocessable_entity }
        format.iphone { render :action => "new" }
      end
    end
  end

  # GET /expenses/1
  # GET /expenses/1.xml
  def show
    @page_title = "Expense: " + @expense.description

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @expense }
      format.iphone do # show.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # GET /expenses/1/edit
  def edit
    @page_title = "Editing: " + @expense.description
    respond_to do |format|
      format.html # edit.html.erb
      format.xml  { render :xml => @expense }
      format.iphone do # edit.iphone.erb
        @panel_title = @page_title
        render :layout => false
      end
    end
  end

  # PUT /expenses/1
  # PUT /expenses/1.xml
  def update
    @expense.categories_expenses.destroy_all
    if (params[:expense][:categories]) 
      params[:expense][:categories].each do |category_id|
        category_id = category_id.to_i
        if category_id > 0
          @expense.categories << Category.find(category_id)
        end
      end
    end

    respond_to do |format|
      if @expense.update_attributes(params[:expense])
        flash[:notice] = 'Expense was successfully updated.'
        format.html { redirect_to(@expense) }
        format.xml  { head :ok }
        format.iphone { redirect_to :controller => "welcome", :action => "home" }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @expense.errors, :status => :unprocessable_entity }
        format.iphone { render :action => "edit" }
      end
    end
  end

  # DELETE /expenses/1
  # DELETE /expenses/1.xml
  def destroy
    @expense.destroy
    # TODO: This flash doesn't seem to be showing up?
    flash[:notice] = "This expense has been permanently removed."

    respond_to do |format|
      format.html { redirect_to(expenses_url) }
      format.xml  { head :ok }
      format.iphone { redirect_to(expenses_url) }
    end
  end

private
  def initialize_expense
    if logged_in?
      @expense = @current_user.expenses.find(params[:id])
      head 404 unless @expense
    else
      flash[:warning] = "You must be logged in to perform that operation."
      redirect_to new_sessions_path
    end
  end

  def initialize_categories
    @categories = Category.find(:all)
  end

end
